package org.gg.boom.common.model;


import org.gg.boom.kernel.constant.HttpStatus;
import org.gg.boom.kernel.exceptions.BizException;

import java.io.Serializable;

/**
 * 响应信息主体
 */
public class Response implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public static final int SUCCESS = HttpStatus.SUCCESS;

    /**
     * 警告
     */
    public static final int WARN = HttpStatus.WARN;

    /**
     * 失败
     */
    public static final int FAIL = HttpStatus.ERROR;

    private int code;

    private String msg;

    private Object data;

    public static Response success() {
        return restResult(null, SUCCESS, "操作成功");
    }

    public static Response success(Object data) {
        return restResult(data, SUCCESS, "操作成功");
    }

    public static Response success(Object data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    public static Response error() {
        return restResult(null, FAIL, "操作失败");
    }

    public static Response error(String msg) {
        return restResult(null, FAIL, msg);
    }

    public static Response error(Object data) {
        return restResult(data, FAIL, "操作失败");
    }

    public static Response error(Object data, String msg) {
        return restResult(data, FAIL, msg);
    }

    public static Response error(int code, String msg) {
        return restResult(null, code, msg);
    }

    public static Response error(BizException bizException) {
        return restResult(null, bizException.getCode(), bizException.getMessage());
    }

    public static Response warn(String msg) {
        return restResult(null, WARN, msg);
    }

    private static Response restResult(Object data, int code, String msg) {
        Response apiResult = new Response();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public static Boolean isError(Response ret) {
        return !isSuccess(ret);
    }

    public static Boolean isSuccess(Response ret) {
        return Response.SUCCESS == ret.getCode();
    }
}
